<!--
 * @Author: ChunLai
 * @Date: 2023-08-30 17:02:55
 * @LastEditTime: 2024-09-24 18:12:41
 * @Description: pdf文件展示公共组件(直接传文件地址)
 * @FilePath: \01.latest-short-insure\src\components\PdfFileDisplay.vue
-->
<template>
  <van-popup
    overlay-class="lay1_shark"
    v-model="showPdfPop"
    position="bottom"
    :style="{ height: '100%' }"
    sticky
  >
    <div class="new-sign-detail">
      <div class="right-fix-btn" v-if="pdfh5">
        <div class="count-down" v-if="countTime > 0">
          {{ `${countTime}s` }}
        </div>
        <div class="read-back" @click="closePdfPop" v-else>我已阅读</div>
      </div>
      <div id="demoPdf"></div>
    </div>
  </van-popup>
</template>

<script>
import { pdfJs } from "@/utils/pdfh5";
export default {
  name: "PdfFileDisplay",
  data() {
    return {
      showPdfPop: false,
      pdfh5: null,
      countTime: 5,
      timeOutPdf: null,
      pdfUrl: "",
    };
  },
  components: {},
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    initModule(fileUrl) {
      this.pdfUrl = fileUrl;
      this.showPdfPop = true;
      let that = this;
      if (window.Pdfh5) {
        return that.initPlugPdf();
      }
      pdfJs(function (res) {
        that.initPlugPdf();
      });
    },
    initPlugPdf() {
      this.$nextTick(() => {
        this.pdfh5 = new Pdfh5("#demoPdf", {
          pdfurl: this.pdfUrl,
        });
        let that = this;
        // 监听完成事件
        this.pdfh5.on("complete", function (status, msg, time) {
          that.pdfPageNum = this.totalNum;
          that.countDownEvent();
        });
      });
    },
    countDownEvent() {
      this.timeOutPdf = setInterval(() => {
        this.countTime--;
        if (this.countTime <= 0) {
          clearInterval(this.timeOutPdf);
        }
      }, 1000);
    },
    closePdfPop() {
      this.$emit("handleClick", this.pdfUrl);
      this.pdfh5 = null;
      this.pdfUrl = "";
      this.timeOutPdf = null;
      this.countTime = 5;
      this.showPdfPop = false;
    },
  },
};
</script>

<style lang="less" scoped>
.new-sign-detail {
  width: 100%;
  margin-top: 10px;
  height: calc(100vh - 10px);

  /deep/.backTop {
    bottom: 60px;
  }
}

.right-fix-btn {
  position: fixed;
  right: 0;
  bottom: 10px;
  z-index: 99;
  color: #ffffff;

  .count-down {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    line-height: 42px;
    text-align: center;
    font-size: 20px;
    background-color: #fb8c89;
  }

  .read-back {
    width: 100px;
    height: 38px;
    line-height: 38px;
    font-size: 16px;
    letter-spacing: 2px;
    text-align: center;
    background-color: #fd6703;
    border-radius: 25px;
  }
}
</style>
